package com.slh.touchandroid.activity;

import com.slh.touchandroid.receiver.DeviceAdminReceiver;
import com.slh.touchandroid.utils.AppUtils;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class DummyActivity extends Activity {
	// ==================================================================
	public static final String ACTION_ADD_DEVICE = "com.slh.touchandroid.receiver.DeviceAdminReceiver.ACTION_ADD_DEVICE";
	public static final String ACTION_BRIGHTNESS = "com.slh.touchandroid.receiver.DeviceAdminReceiver.ACTION_BRIGHTNESS";

	// ==================================================================
	public static void start(Context context, String action) {
		Intent intent = new Intent(context, DummyActivity.class);
		intent.setAction(action);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String action = getIntent().getAction();
		if (ACTION_ADD_DEVICE.equals(action)) {
			requestAddDevice();
		} else if (ACTION_BRIGHTNESS.equals(action)) {
			finish();
		}
	}

	// ==================================================================
	private void requestAddDevice() {
		DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
		ComponentName compName = new ComponentName(getApplicationContext(),
				DeviceAdminReceiver.class);
		if (devicePolicyManager.isAdminActive(compName)) {
			devicePolicyManager.lockNow();
			return;
		}
		Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
		startActivity(intent);
	}
	// ==================================================================
}
